<?php

/**
 * 用户管理
 */
namespace app\member\model;

use app\system\model\SystemModel;

class MemberUserModel extends SystemModel {

    protected $infoModel = [
        'pri' => 'user_id',
        'validate' => [
            'role_id' => [
                'empty' => ['', '请选择角色', 'must', 'all'],
            ],
            'email' => [
                'email' => ['', '邮箱填写不正确', 'must', 'all'],
                'unique' => ['', '已存在相同的用户名', 'must', 'all'],
            ],
            'tel' => [
                'phone' => ['', '手机号码填写不正确', 'must', 'all'],
                'unique' => ['', '已存在相同的用户名', 'must', 'all'],
            ],
            'nickname' => [
                'len' => ['2,20', '昵称只能为2~20个字符', 'must', 'all'],
                'unique' => ['', '已存在相同的用户名', 'must', 'all'],
            ],
            'password' => [
                'len' => ['3,50', '请输入3~50位密码', 'must', 'add'],
            ]
        ],
        'format' => [
            'password' => [
                'ignore' => ['', 'edit'],
            ],
            'reg_time' => [
                'function' => ['time', 'add'],
            ]
        ],
        'into' => '',
        'out' => '',
    ];

    protected function _saveBefore($data) {
        if($data['password']) {
            $data['password'] = md5($data['password']);
        }
        return $data;
    }

    protected function base($where) {
        return $this->table('member_user(A)')
            ->join('member_role(B)', ['B.role_id', 'A.role_id'])
            ->field(['A.*', 'B.name(role_name)'])
            ->where((array)$where);
    }

    public function loadList($where = array(), $limit = 0, $order = '') {
        $list = $this->base($where)
            ->limit($limit)
            ->order($order)
            ->select();
        return $list;
    }

    public function countList($where = array()) {
        return $this->base($where)->count();
    }

    public function getWhereInfo($where) {
        return $this->base($where)->find();
    }



}